
function workerInfoCtrl($q, $rootScope, $scope, $http, ngDialog, paFetchService) {
  'ngInject';

  // 查询条件的对象
  this.userInfo = {};

  // 分页
  this.paginationData = {
    pageNum: 1,
    pageSize: 10,
    total: '',
  }

  this.beforeQuery4Department = () => {
    let _this = this;
    return paFetchService.getTeamWithOrgList()
            .then((respData) => {
              let teamsData = respData.data;
              let activeOrgId = $rootScope.activeBid.orgId;
              teamsData.forEach((org) => {
                if (org.orgUnitId == activeOrgId) {
                  org.teamList.forEach((team) => {
                    if (team.teamName == '项目部') {
                      _this.userInfo.constructionTeamId = team.teamId;
                    }
                  })
                }
              })
            })
  }

  this.beforeQuery4SpecialWorker = () => {
    let deferred = $q.defer()
    this.userInfo.isspecialMan = '1';
    deferred.resolve();
    return deferred.promise;
  }

  // 页码变化时调用方法
  $scope.pageChange = () => {
    // this.userInfo.orgId = $rootScope.activeBid.orgId;
    $http.post(`${SERVER_CONTEXT}/api/safesite/workerInfo/selectByPage`,
      angular.extend(this.userInfo, this.paginationData))
      .then((resp) => {
        this.workerItemList = resp.data.data;
        this.paginationData.total = resp.data.total;
    });
  };

  // 分页查询方法
  this.paginationQuery = () => {
    // this.userInfo.orgId = $rootScope.activeBid.orgId;
    $http.post(`${SERVER_CONTEXT}/api/safesite/workerInfo/selectByPage`,
      angular.extend(this.userInfo, this.paginationData)
        )
      .then((resp) => {
        this.workerItemList = resp.data.data;
        this.paginationData.total = resp.data.total;
    });
  };

  // 重置
  this.reset = () => {
    this.userInfo = {};
  }

  // 查询
  this.query = () => {
    this.paginationQuery();
  }

  let getSelectedWorkers = () => {
    let selectedWorkerList = [];
    let length = this.workerItemList.length;
    for (let i = 0; i < length; i++) {
      let item = this.workerItemList[i];
      if (item.checked) {
        selectedWorkerList.push(item);
      }
    }
    return selectedWorkerList;
  }

  // 离场登记
  this.forRegistration = () => {
    let selectedWorkerList = [], leaveTimeList = [], hasCheckedItem = false,
      shouldContinue = true;
   
    // 获得选中的人员
    let length = this.workerItemList.length;
    for (let i = 0; i < length; i++) {
      let item = this.workerItemList[i];
      if (item.checked) {
        shouldContinue = item.leaveTime ? false : true;
        selectedWorkerList.push(item);
      }
      if (!shouldContinue) {
        layer.alert('选择项内有已出场人员,请重新选择');
        return false;
      }
    }
    if (!selectedWorkerList.length) {
      layer.alert('请选择出场人员');
      return false;
    }

    $scope.selectedWorkerList = selectedWorkerList;
    $scope.leaveTimeList = leaveTimeList;

    let _this = this;
    ngDialog.openConfirm({
      template: 'workerExitDialog',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
      controllerAs: 'vm',
      controller: function($http, $scope) {
        'ngInject';
        this.submit = () => {
          let outPlantTime = this.outPlantTime;
          $scope.selectedWorkerList.forEach((item) => {
            item.leaveTime = outPlantTime;
            leaveTimeList.push(item.id);
          });

          let reqData = {
            ids : leaveTimeList,
            leaveTime : outPlantTime
          }; 

          $http({
            method: 'post',
            url: `${SERVER_CONTEXT}/api/safesite/workerInfo/leavesite`,
            data: reqData,
          }).then((resp) => {
            if (resp.statusText == 'OK') {
              $scope.workerInfo.paginationQuery();
              $scope.closeThisDialog();
              layer.msg('出场成功');
            } else {
              $scope.closeThisDialog();
              layer.alert('服务器错误,请联系技术人员');
            }
          });
        }
      },
    })
  };

  // 打印二维码
  this.printQrCode = () => {
    let selectedWorkerList = getSelectedWorkers();
    if (!selectedWorkerList.length) {
      layer.alert('请选择人员');
      return;
    }
    $scope.selectedWorkerList = selectedWorkerList;
    ngDialog.openConfirm({
      template: 'workerInfoQrCodePrintDialog',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
      controllerAs: 'vm',
      controller: function($scope) {
        'ngInject';
        this.print = () => {
          $('.qr-print').print();
        }

        this.getQrCodeSrc = (item) => {
          let qrContent = encodeURIComponent(`#/workerInfo/${item.id}`);
          let requestUrlStr = 
          `${$scope.hostUrl}${$scope.serverContext}/api/qr?size=160&code=${qrContent}`;
          return requestUrlStr;
        }
      },
    })
  }

  this.toggleAllFlag = false;

  // 全选
  this.toggleAll = () => {
    this.workerItemList.forEach((item) => {
      item.checked = this.toggleAllFlag;      
    });
  }

  // 判断全选人
  this.isChange = () => {
    var bool = true;
    this.workerItemList.forEach((item) => {
      if(!item.checked){
        bool = false;
      }
    });
    this.toggleAllFlag = bool;        
  }

  // 删除人员
  this.delete = (id) => {
    $scope.modalTitle = '删除人员';
    $scope.modalContent = '确认要删除该人员信息吗?';
    ngDialog.openConfirm({
      template: 'paConfirm',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
    }).
      then((value) => {
        $http.get(`${SERVER_CONTEXT}/api/safesite/workerInfo/delete/${id}`).
          then((resp) => {
            if (resp.data.success) {              
              layer.msg(resp.data.message);
              this.paginationQuery();
            } else {
              layer.alert(resp.data.message);
            }         
          });
      }, (reason) => {});
  }

  // 升序、降序
  this.up = (param,index) => {   
    this.userInfo.columeName = param;
    this.userInfo.order = index;
    this.paginationQuery();
  }

  // 初始化页面信息
  this.initPage = () => {   
    // 获得施工队list             
    paFetchService
      .getTeamWithOrgList()
      .then((resp) => {
        this.teamWithOrgList = resp.data;
      });
    // this.paginationQuery();
  }

  this.initPage();

}

export default workerInfoCtrl;